<?php
function stripUnicode($str){
	if(!$str) return false;
	$unicode = array(
	'a'=>'Ã¡|Ã |áº£|Ã£|áº¡|Äƒ|áº¯|áº·|áº±|áº³|áºµ|Ã¢|áº¥|áº§|áº©|áº«|áº­',
	'A'=>'Ã�|Ã€|áº¢|Ãƒ|áº |Ä‚|áº®|áº¶|áº°|áº²|áº´|Ã‚|áº¤|áº¦|áº¨|áºª|áº¬',
	'd'=>'Ä‘',
	'D'=>'Ä�',
	'e'=>'Ã©|Ã¨|áº»|áº½|áº¹|Ãª|áº¿|á»�|á»ƒ|á»…|á»‡',
	'E'=>'Ã‰|Ãˆ|áºº|áº¼|áº¸|ÃŠ|áº¾|á»€|á»‚|á»„|á»†',
	'i'=>'Ã­|Ã¬|á»‰|Ä©|á»‹',
	'I'=>'Ã�|ÃŒ|á»ˆ|Ä¨|á»Š',
	'o'=>'Ã³|Ã²|á»�|Ãµ|á»�|Ã´|á»‘|á»“|á»•|á»—|á»™|Æ¡|á»›|á»�|á»Ÿ|á»¡|á»£',
	'O'=>'Ã“|Ã’|á»Ž|Ã•|á»Œ|Ã”|á»�|á»’|á»”|á»–|á»˜|Æ |á»š|á»œ|á»ž|á» |á»¢',
	'u'=>'Ãº|Ã¹|á»§|Å©|á»¥|Æ°|á»©|á»«|á»­|á»¯|á»±',
	'U'=>'Ãš|Ã™|á»¦|Å¨|á»¤|Æ¯|á»¨|á»ª|á»¬|á»®|á»°',
	'y'=>'Ã½|á»³|á»·|á»¹|á»µ',
	'Y'=>'Ã�|á»²|á»¶|á»¸|Y',
	);
	foreach($unicode as $nonUnicode=>$uni)
	$str = preg_replace("/($uni)/i",$nonUnicode,$str);
	return $str;
}
//function use for autocomplete
function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".$key."\"";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".$value."\"";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".$value."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}
//end function 
function pageString($rows,$pagesize)//$row lÃ  tá»•ng sá»‘ dÃ²ng cá»§a data
{
	$pages = ceil($rows/$pagesize);
	$url = $_SERVER['REQUEST_URI'];
	$page_now = isset($_GET['page']) ? $_GET['page'] : 1;
	$str_page = "page=".$page_now;
	if($page_now>$pages || $page_now<=0 || !is_numeric($_GET['page']))	$page_now = 1;	
	$str = '';
	$str_page_begin = ($page_now > 4) ? ($page_now-3) : 1;
	if($str_page_begin>1)
	{
		$url_temp = str_replace($str_page,"page=1",$url);
		$str .= "<a href='".$url_temp."'>Ä�áº§u </a>";
		$page_pre = $page_now - 1;
		$url_page_pre = str_replace($str_page,"page=$page_pre",$url);
		$str .= "<a href='".$url_page_pre."'> TrÆ°á»›c </a>";		
	}
	$str_page_end = (($page_now + 3)<$pages) ? ($page_now+3) : $pages;
	for($i=$str_page_begin;$i<=$str_page_end;$i++)
	{
		if($page_now>1)	
		{
			$url_temp = str_replace($str_page,"page=$i",$url);
		}
		else
		{
			if(strpos($url,'?')>0)
				$url_temp = isset($_GET['page']) ? str_replace($str_page,"page=$i",$url) : $url."&page=$i";
			else
				$url_temp = $url."?page=$i";
		}
		if($i==$page_now)
			$str .= "<a class='pagenow' href='".$url_temp."'><b> ".$i." </b></a>";
		else
			$str .= "<a href='".$url_temp."'> ".$i." </a>";
	}
	if($str_page_end<$pages)
	{
		$page_next = $page_now + 1;
		if(isset($_GET['page']))
		{
			$url_page_next = str_replace($str_page,"page=$page_next",$url);
			$url_temp = str_replace($str_page,"page=$pages",$url);
		}
		else
		{
			$url_page_next = (strpos($url,'?')>0) ? $url."&page=$page_next" : $url_temp = $url."?page=$page_next";
			$url_temp = (strpos($url,'?')>0) ? $url."&page=$pages" : $url_temp = $url."?page=$pages";				
		}
		$str .= "<a href='".$url_page_next."'> Sau </a>";		
		$str .= "<a href='".$url_temp."'> Cuá»‘i</a>";
	}	
	$offset = ($page_now - 1) * $pagesize;
	$result = array($pagesize,$offset,$str);
	return $result;
}
    function escape ($value)
    {
        if( get_magic_quotes_gpc() ) {
            $value = stripslashes( $value );
        }
        if(function_exists( "mysql_real_escape_string" ) ) { // mysql_real_escape_string is exist
            $value = @mysql_real_escape_string( $value );
        } else { // or
            $value = addslashes( $value );
        }
        return $value;
    }
    function intDateExcelToSql($dateExcel)
    {
		return mktime(0,0,0,1,$dateExcel-1,1900);
    }
    function stringDateExcelToSql($dateExcel)
    {
		return date("Y-m-d",mktime(0,0,0,1,$dateExcel-1,1900));
    }    
	function stringGioiTinh($n)
	{
		$str = ($n ==1) ? "Nam" : "Ná»¯";
		return $str;
	}
	function intGioiTinh($str)
	{
		$i = (strtolower($str) === "nam") ? 1 : 0;
		return $i;
	}	
	function ddMmYy($strYyMmDd)
	{
		return date('d-m-Y',strtotime($strYyMmDd));
	}
	function yyMmDd($strDdMmYy)
	{
		return date('Y-m-d',strtotime($strDdMmYy));
	}	
	function strToPhone($strPhone)
	{
		$arr = array('(',') ','-',);
		return str_replace($arr,'',$strPhone);
	}	
function current_link_menu($links) 
{
	$html = '';
	$currentUrl = $_SERVER['REQUEST_URI'];
	foreach($links AS $l=>$n) {
//		if (trim($l,'/')==trim($link,'/')) {

		if (trim($l,'/')==trim($link,'/')) {
			$html .= "<li class=\"current\"><a href=\"{$l}\">{$n}</a></li>";
		}
		else $html .= "<li><a href=\"{$l}\">{$n}</a></li>";
	}
	return $html;
}

?>